package com.senmiao.ssm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.senmiao.ssm.entity.Admin;
import com.senmiao.ssm.entity.AdminRole;
import com.senmiao.ssm.entity.Role;
import com.senmiao.ssm.exception.MyException;
import com.senmiao.ssm.mapper.AdminMapper;
import com.senmiao.ssm.mapper.AdminRoleMapper;
import com.senmiao.ssm.mapper.RoleMapper;
import com.senmiao.ssm.pojo.vo.AdminInfoVo;
import com.senmiao.ssm.service.CommonService;
import com.senmiao.ssm.utils.EncryptUtils;
import com.senmiao.ssm.utils.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CommonServiceImpl implements CommonService {

    @Resource
    private AdminMapper adminMapper;

    @Resource
    private AdminRoleMapper adminRoleMapper;

    @Resource
    private RoleMapper roleMapper;

    /**
     * 根据id获取管理员信息
     *
     * @param id
     * @return
     */
    @Override
    public Result getAdminInfo(Integer id) {
        Admin admin = adminMapper.selectById(id);
        if (admin != null) {
            AdminInfoVo adminInfoVo = new AdminInfoVo();
            adminInfoVo.setId(admin.getId());
            adminInfoVo.setEmail(admin.getEmail());
            adminInfoVo.setPhone(admin.getPhone());
            adminInfoVo.setUsername(admin.getUsername());
            List<AdminRole> roles = adminRoleMapper.selectList(new QueryWrapper<AdminRole>()
                    .eq("admin_id", id).eq("is_delete", 0));
            ArrayList<Integer> roleIds = new ArrayList<>();
            roles.forEach(e->roleIds.add(e.getRoleId()));
            adminInfoVo.setRoleIds(roleIds);
            return Result.ok().data("data", adminInfoVo);
        }
        throw new MyException("管理员id不存在");
    }

    @Override
    public Result updatePassword(Integer id, String currentPassword, String newPassword) {

        Admin admin = adminMapper.selectOne(new QueryWrapper<Admin>().eq("id",id).eq("is_delete",0));
        if (admin == null) {
            throw new MyException("管理员id不存在");
        }
        // 对用户输入的原密码与数据库中的密码进行比较
        if (!admin.getPassword().equals(EncryptUtils.encryptPassword(currentPassword, admin.getSalt()))) {
            throw new MyException("输入的当前密码错误");
        }
        // 生成盐
        String salt = EncryptUtils.generateSalt();
        // 对用户输入的密码进行加密
        String encryptPassword = EncryptUtils.encryptPassword(newPassword, salt);
        admin.setPassword(encryptPassword);
        admin.setSalt(salt);
        admin.setModifiedTime(new Date());
        int update = adminMapper.update(admin, new QueryWrapper<Admin>().eq("id", admin.getId()));
        if (update < 1) {
            throw new MyException("更新数据库异常");
        }
        return Result.ok();
    }

    @Override
    public Result updateInfo(Integer id, String username, String phone, String email) {
        Admin admin = adminMapper.selectById(id);
        if (admin == null) {
            throw new MyException("管理员id不存在");
        }
        if (!StringUtils.isEmpty(username)) {
            admin.setUsername(username);
        }
        if (!StringUtils.isEmpty(phone)) {
            admin.setPhone(phone);
        }
        if (!StringUtils.isEmpty(email)) {
            admin.setEmail(email);
        }
        admin.setModifiedTime(new Date());
        int update = adminMapper.update(admin, new QueryWrapper<Admin>().eq("id", admin.getId()));
        if (update < 1) {
            throw new MyException("更新数据库异常");
        }
        return Result.ok();
    }

    @Override
    public String getForwardUrlByAdminId(Integer adminId) {
        AdminRole adminRole = adminRoleMapper.selectOne(new QueryWrapper<AdminRole>()
                .eq("admin_id", adminId).eq("is_delete", 0));
        if (adminRole == null){
            return "error.html";
        }
        Role role = roleMapper.selectOne(new QueryWrapper<Role>().eq("id", adminRole.getRoleId()).eq("is_delete", 0));
        if (role == null){
            return "error.html";
        }
        String name = role.getName();
        String url = "error.html";
        if ("系统管理员".equals(name)){
            url = "systemManager.html";
        }else if ("安全保密员".equals(name)){
            url = "securitySecrecyManager.html";
        }else if ("安全审计员".equals(name)){
            url = "auditManager.html";
        }else if ("业务管理员".equals(name)){
            url = "businessManager.html";
        }
        return url;
    }
}